<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 16.11.11
 * Time: 17:31
 * To change this template use File | Settings | File Templates.
 */

class Core_Loader
{
    private $variable = array();
    public static $includePath = array();
    static $deprecatedPath = array('.', '..', '.svn');
    private static $instance = null;

    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new Core_Loader();
        }
        return self::$instance;
    }

    public function __set($name, $value)
    {

        $this->variable[$name] = $value;
    }

    public function __get($name)
    {
        if (isset($this->variable[$name])) {
            return $this->variable[$name];
        }
        throw new Exception("Variable " . $name . " not setted !");
    }

    public function __construct()
    {
    }

    public function addLoader($loader)
    {
        spl_autoload_register($loader);

    }

    public function register()
    {
        spl_autoload_register(array($this, 'loader'));


    }

    function myLoaderTwo($class)
    {
        include "/Alllibs/" . $class . ".php";

    }

    /**
     * @param $path
     * @param int $level
     * @return array
     */
    public static function  dirList($path, $level = 0)
    {
        $dh = @opendir($path);

        while (false !== ($file = readdir($dh))) {
            if (!in_array($file, self::$deprecatedPath)) {

                if (is_dir("$path" . DS . "$file")) {
                    self::$includePath[] = "$path" . DS . "$file";
                    self::dirList("$path" . DS . "$file", ($level + 1));

                }
            }
        }
        closedir($dh);

    }

    public static function addLayoutsPaths($paths)
    {
        self::$layoutPaths = $paths;
        foreach ($paths as $path) {
            self::dirList($path);
        }
    }

    public static function addIncludePath($paths)
    {
        self::$includePath = $paths;
        foreach ($paths as $path) {
            self::dirList($path);
        }
    }

    private function isReadable($className)
    {
        //app/lib/My/Core/Path/ClassTwo.php
        //app/lib2/My/Core/Path/ClassTwo.php
        foreach (self::$includePath as $path) {
            $file = $path . DS . $className . '.php';

            if (is_readable($file)) {
                return $file;
            } else continue;
        }
        return false;
    }

    public function loader($class)
    {
        //$class = My_core_Path_ClassTwo

        $className = explode('_', $class);
        $file = '';
        for ($i = 0; $i < count($className) - 1; $i++) {
            $file = $file . $className[$i] . DS;
            //   My/Core/Path/ClassTwo
        }
        $file = $file . end($className);
        $file = $this->isReadable($file);
        if ($file != false) {
            if (!true)
                echo "<pre><span style=\"color:red;\">[DEBUG]Core_Loader->loader() </span>Load File:" . $file . "</pre></br>";
            include $file;
        } else {
            echo "<pre>";
            throw new Exception("Class: " . $file . "not found");
            echo "</pre>";
        }
        $myObject = new Class_name();

        // 1. loader(Class_name)
        // 2. include (app/lib2/Class/name.php)
        // 3. new  Class_name();
        // 4. return object
        // 5.  $myObject = object;

    }

}
